package com.bailian.service;

import com.bailian.core.utils.BeanCopyUtils;
import com.bailian.core.utils.RedisUtil;
import com.bailian.mapper.UserMapper;
import com.bailian.mapper.entity.UserDo;
import com.bailian.member.IMemberService;
import com.bailian.member.dto.input.UserLoginInpDTO;
import com.bailian.member.dto.output.UserOutDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberServiceImpl extends BaseApiService<UserOutDTO> implements IMemberService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public BaseResponse<UserOutDTO> existMobile(String mobile) {
        final UserDo userDo = userMapper.existMobile(mobile);
        return setResultSuccess(BeanCopyUtils.doToDto(userDo,UserOutDTO.class));
    }

    @Override
    public BaseResponse<UserOutDTO> getInfo(String token) {
        final String userId = redisUtil.getString(token);
        if (StringUtils.isEmpty(userId)) return setResultError("用户登录状态已失效，请重新登录");
        final UserDo user = userMapper.findByUserId(Long.valueOf(userId));
        if (user == null) return setResultError("用户不存在");
        final UserOutDTO userOutDTO = BeanCopyUtils.doToDto(user, UserOutDTO.class);
        return setResultSuccess(userOutDTO);
    }

    @Override
    public BaseResponse<UserOutDTO> ssoLogin(@RequestBody @Validated UserLoginInpDTO userLoginInpDTO) {
        return null;
    }
}
